import media from '@ohos.multimedia.media';
import router from '@ohos.router';
import {Cache} from '../../common/utils/Common';

export default {
    data: {
        // 记录旋转的位置
        deg: 0,
        // 记录执行器
        playInterval: null,
        // 路由
        routerPath: router.getState().path
    },
    props: {
        playId: {
            type: Number
        },
        playCover: {
            type: String,
            default: 'https://p1.music.126.net/8g2Pn9Xw1m7cFhxOlc3BMg==/109951167875709379.jpg?param=140y140'
        },
        playTitle: {
            type: String,
            default: '留言'
        },
        playSinger: {
            type: String,
            default: '张学友'
        },
        playFunc: {
            type: Boolean,
            default: true
        }
    },
    onInit() {
        // 从缓存中获取播放的记录
        let cache = this.$app.$def.cache;
        if (!cache) {
            cache = new Cache();
            this.$app.$def.cache = cache;
        }
        let currentPlay = cache.get('currentPlay');
        if (currentPlay) {
            this.playId = currentPlay.id;
            this.playCover = currentPlay.cover;
            this.playTitle = currentPlay.title;
            this.playSinger = currentPlay.singer;
            this.playFunc = currentPlay.playing;
        }

        let currentDeg = cache.get('currentDeg');
        if (currentDeg) {
            this.deg = currentDeg;
        }
    },
    onReady(){
        this.$watch('playCover', this.onPlayCover);
        this.$watch('playId', this.onPlayId);
        this.$watch('playFunc', this.onPlayFunc);
        let cache = this.$app.$def.cache;
        this.$refs.musicInner.setStyle('background-image', this.deg);
        let currentDeg = cache.get('currentDeg');
        let musicInner;
        if (currentDeg && (musicInner = this.$refs.musicInner)) {
            this.deg = currentDeg;
            musicInner.setStyle('background-image', currentDeg);
        }
        if (this.playFunc) {
            this.playMusic();
        } else {
            this.stopMusic();
        }
    },
    onPlayFunc(n) {
        if (n) {
            this.playMusic();
        } else {
            this.stopMusic();
        }
    },
    onPlayId(n, o) {
        if (n != o) {
            this.deg = 0;
        }
    },
    onPlayCover(n) {
        this.$refs.musicInner.setStyle('background-image', n);
    },
    /**
     * 路由跳转
     * @param mode 0: 首页 | 1: 音乐列表 | 2: 个人中心
     */
    getPage(mode){
        if (mode == 0) {
            router.push({
                url: 'pages/index/index'
            });
        } else if (mode == 1) {
            router.push({
                url: 'pages/music_list/music_list',
                params: {mode: 0}
            });
        } else if (mode == 2) {
            router.push({
                url: 'pages/myself/myself'
            });
        }
    },
    playMusic() {
        let ref = this.$refs.musicInner;
        this.playFunc = true;
        this.playInterval = setInterval(() => {
            ref.setStyle('transform', `rotate(${this.deg}deg)`);
            this.deg = this.deg + 1;
            // 播放每次都记录 deg
            this.saveDeg();
        }, 50);
        // 播放之后把当前播放的音乐 id 返回给父组件
        this.$emit('onPlay', this.playId);
    },
    stopMusic() {
        if (this.playInterval) {
            clearInterval(this.playInterval);
        }
        this.playFunc = false;
        this.saveDeg();
        // 停止播放也把 id 返回给父组件
        this.$emit('onStop', this.playId);
    },
    saveDeg() {
        let cache = this.$app.$def.cache;
        if (cache) {
            cache.set('currentDeg', this.deg);
        }
    }
}